<?php
/**
 * Created by PhpStorm.
 * User: castle
 * Date: 15-4-13
 * Time: 下午8:49
 */
namespace Repair\Model;
use Think\Model;

class OrderModel extends  Model{
    protected $_map = array(
        'OrderID'=>'orderid',
'Model'=>'model',
'Color'=>'color',
'Contact'=>'contact',
'Phone'=>'phone',
'Address'=>'address',
'AccessoriesID'=>'accessoriesid',
'Phenomenon'=>'phenomenon',
'Memo'=>'memo',
'Status'=>'status',
'Time'=>'time',

	);
	//获取单个信息
    function get($OrderID){
        //此处要判断是否有APPID，如果有APPID则在此处加上APPID相关信息
        if(is_numeric($OrderID)){return $this->Order($this->where(array('OrderID'=>$OrderID,))->find());}return false;
    }
    //获取所有信息
    function gets($OrderID = false){
        if(false === $OrderID ){
            return $this->Order($this->where(array())->cache(7200)->select());
        }else if(is_array($OrderID)){
            return $this->Order($this->where('OrderID in ('.implode(',',$OrderID).')')->select());
        }else{
            return false;
        }
    }
    function addOrder(){
       return $this->add($_POST);
    }
    function getOrderByStatus($Status,$Page,$PageNumber){

        if(strlen($Status)==0){
            $w = [];
        }
        elseif(is_numeric($Status)){
            $w['Status'] = $Status;
        }
        elseif(is_array($Status)){
            $w = 'Status in ('.implode(',',$Status).')';
        }elseif(is_string($Status)){
            $w = 'Status in ('.explode(',',$Status).')';
        }
        else{
            return false;
        }
        $json['List'] = $this->Order($this->where($w)->page($Page,$PageNumber)->select());
        $json['Page'] = $Page;
        $json['PageNumber']=$PageNumber;
        $json['Total']=$this->where($w)->field('count(OrderID) as sum')->find()['sum'];
        return $json;
    }
    function Order($Order){
        //一维
        $Double = true; //记录变量是否为一维数组
        if(isset($Order['OrderID'])){ //如果是一位数组则转化为多维数组并做好记录
            $Double=false;
            $Order = array($Order);
        }
        //获取配件编号列表
        $AccessoriesIDs = [];
        foreach($Order as $k=>$v){ //提取所有的配件编号
            if(strlen($v['AccessoriesID'])>0){
                $AccessoriesIDs = array_merge($AccessoriesIDs,explode(',',$v['AccessoriesID'])); //将所有配件编号组合成英文逗号分隔的字符串
            }
        }
        if(count($AccessoriesIDs)!=0){
            //执行查询
            $AccessoriesModel = new AccessoriesModel();  //实例化配件模型
            $rs = $AccessoriesModel->where('AccessoriesID in ('.implode(',',$AccessoriesIDs).')')->field('AccessoriesID,Name,Model,Number')->select();//执行配件查询
            $list = [];
            foreach($rs as $v){
                $list[$v['AccessoriesID']]=$v; //进行数组格式转化，原数组键与值无关，转化成有关数组
            }
            $rs = $list;
            //获取工单数据
            $LogModel = new LogModel();  //实例化订单处理记录模型
            //组合生成数据
            if(is_array($rs)){
                foreach($Order as $k=>$v){
                    if($v['AccessoriesID']){
                        foreach(explode(',',$v['AccessoriesID']) as $AccessoriesID){
                            $Order[$k]['Accessories'][] = $rs[$AccessoriesID]; // 提取所有该订单的配件列表写入到订单配件属性中
                        }
                    }
                    //获得处理记录
                    $Order[$k]['Log'] = $LogModel->where(array('OrderID'=>$v['OrderID']))->select();//查询该订单的处理记录并写入到订单中
                    //处理UID和UN
                    foreach($Order[$k]['Log'] as $key=>$Log){
                        $Order[$k]['Log'][$key]['UID'] = explode(',',$Log['UID']);//将UID和UN的数据转化为可识别的
                        if(!$Order[$k]['T'.$Log['Type']])
                            $Order[$k]['T'.$Log['Type']] = ['date'=>$Log['Time'],'Content'=>$Log['Content'],'UN'=>explode(',',$Log['UID'])[1]];
                    }
                }
            }
        }


        if($Double)
            return $Order;
        else
            return $Order[0];
    }
}
?>
